package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.entity.Company;
import com.xishu.entity.shop.LineUpConfig;
import com.xishu.entity.shop.Shop;
import com.xishu.exception.ResponseStatusException;
import com.xishu.response.ResponseData;
import com.xishu.response.ResponseStatus;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.CompanyService;
import com.xishu.service.ShopService;
import com.xishu.util.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(description = "排队配置")
public class LineUpConfigController implements Tools {
    private static Logger logger = LoggerFactory.getLogger(LineUpConfigController.class);

    @ApiOperation(value = "创建排队配置")
    @PostMapping(value = ("/user/lineUpConfig"), produces = ("application/json;charset=UTF-8"))
    public ResponseData create(@RequestBody LineUpConfig lineUpConfig) throws Exception {
        List<LineUpConfig> lineUpConfigs = getLineUpConfigsByShopId(lineUpConfig);
        checkLineUpIsValid(lineUpConfig,lineUpConfigs);
        return CommonService.getInstance().createObject(lineUpConfig);
    }

    /**
     * 根据shopId查询队列
     * @param lineUpConfig
     * @return
     */
    private List<LineUpConfig> getLineUpConfigsByShopId(LineUpConfig lineUpConfig) {
        Long shopId = lineUpConfig.getShopId();
        LineUpConfig entity = new LineUpConfig();
        entity.setShopId(shopId);
        List<LineUpConfig> lineUpConfigs = CommonService.getInstance().searchList(entity);
        return lineUpConfigs;
    }

    @ApiOperation(value = "修改排队配置")
    @PutMapping(value = ("/user/lineUpConfig/modify"), produces = ("application/json;charset=UTF-8"))
    public ResponseData modify(@RequestBody LineUpConfig lineUpConfig) throws Exception {
        Long id = lineUpConfig.getId();
        LineUpConfig entity = CommonService.getInstance().findEntity(lineUpConfig);
        List<LineUpConfig> lineUpConfigs = getLineUpConfigsByShopId(entity);
        List<LineUpConfig> collect = lineUpConfigs.stream().filter(l -> {
            return !equals(l.getId(),id);
        }).collect(Collectors.toList());
        checkLineUpIsValid(lineUpConfig, collect);
        return CommonService.getInstance().updateObject(lineUpConfig);
    }

    /**
     * 判断队列是否合法
     * @param lineUpConfig
     * @param collect
     * @throws ResponseStatusException
     */
    private void checkLineUpIsValid(LineUpConfig lineUpConfig, List<LineUpConfig> collect) throws ResponseStatusException {
        for (LineUpConfig upConfig : collect) {
            if(equals(upConfig.getCode(), lineUpConfig.getCode())){
                VerifyUtil.throwError(ResponseStatus.QUEUE_PREFIX_EXISTS);
            }
            if(equals(upConfig.getMaxLinePersons(), lineUpConfig.getMaxLinePersons())){
                VerifyUtil.throwError(ResponseStatus.QUEUE_CAPACITY_EXISTS);
            }
        }
    }


    @ApiOperation(value = "查询排队配置")
    @PutMapping(value = ("/user/lineUpConfig"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(mustLogin = false)
    public ResponseData search(@RequestBody LineUpConfig lineUpConfig) throws Exception {
        return CommonService.getInstance().searchResponse(lineUpConfig, (l) -> {
            try {
                Long shopId = l.getShopId();
                Shop shop = ShopService.getInstance().findShopById(shopId);
                l.setShopName_zh(shop.getName_zh());
                l.setShopName_en(shop.getName_en());

                Long companyId = shop.getCompanyId();
                Company company = CompanyService.getInstance().findCompanyById(companyId);
                l.setCompanyName(company.getName());
            } catch (Exception e) {
                logger.error("e", e);
            }
        });
    }

    @ApiOperation(value = "修改排队配置")
    @DeleteMapping(value = ("/user/lineUpConfig"), produces = ("application/json;charset=UTF-8"))
    public ResponseData delete(@RequestBody LineUpConfig lineUpConfig) throws Exception {
        return CommonService.getInstance().delete(lineUpConfig);
    }

}
